package com.lastoffer2.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.location.Location;
import android.util.Log;

public class GeoUtil {
	
	private static final String GMAPS_IMAGE_URL="http://maps.googleapis.com/maps/api/staticmap?";
	
	public static String getGMapUrl(int width,int height,double latitude,double longitude,String label,boolean gpsOn){
		return GMAPS_IMAGE_URL+"size="+width+"x"+height+"&scale=2&markers=color:red%7Clabel:"+label+"%7C"+latitude+","+longitude+"&sensor="+gpsOn;
	}
	
	
	
	
	public static String getDistance(Location src, Location dest)
			throws Exception {

		StringBuilder urlString = new StringBuilder();
		urlString
				.append("http://maps.googleapis.com/maps/api/directions/json?");
		urlString.append("origin=");// from
		urlString.append(Double.toString((double) src.getLatitude()));
		urlString.append(",");
		urlString.append(Double.toString((double) src.getLongitude()));
		urlString.append("&destination=");// to
		urlString.append(Double.toString((double) dest.getLatitude()));
		urlString.append(",");
		urlString.append(Double.toString((double) dest.getLongitude()));
		urlString.append("&mode=walking&sensor=true");
		Log.d("xxx", "URL=" + urlString.toString());

		// get the JSON And parse it to get the directions data.
		HttpURLConnection urlConnection = null;
		URL url = null;

		url = new URL(urlString.toString());
		urlConnection = (HttpURLConnection) url.openConnection();
		urlConnection.setRequestMethod("GET");
		urlConnection.setDoOutput(true);
		urlConnection.setDoInput(true);
		urlConnection.connect();

		InputStream inStream = urlConnection.getInputStream();
		BufferedReader bReader = new BufferedReader(new InputStreamReader(
				inStream));

		String temp, response = "";
		while ((temp = bReader.readLine()) != null) {
			// Parse data
			response += temp;
		}
		// Close the reader, stream & connection
		bReader.close();
		inStream.close();
		urlConnection.disconnect();

		// Sortout JSONresponse
		JSONObject object = (JSONObject) new JSONTokener(response)
				.nextValue();
		JSONArray array = object.getJSONArray("routes");
		// Log.d("JSON","array: "+array.toString());

		// Routes is a combination of objects and arrays
		JSONObject routes = array.getJSONObject(0);
		// Log.d("JSON","routes: "+routes.toString());

		String summary = routes.getString("summary");
		// Log.d("JSON","summary: "+summary);

		JSONArray legs = routes.getJSONArray("legs");
		// Log.d("JSON","legs: "+legs.toString());

		JSONObject steps = legs.getJSONObject(0);
		// Log.d("JSON","steps: "+steps.toString());

		JSONObject distance = steps.getJSONObject("distance");
		// Log.d("JSON","distance: "+distance.toString());

		String sDistance = distance.getString("text");
		int iDistance = distance.getInt("value");

		return sDistance;

	}
	
}
